package com.syn.synold4s.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.gaoyi.basemodule.baseAdapter.BaseAdapter;
import com.gaoyi.basemodule.baseAdapter.BaseViewHolder;
import com.syn.synold4s.R;
import com.syn.synold4s.bean.dto.LockDto;
import com.syn.synold4s.databinding.LockItemBinding;

public class LockAdapter extends BaseAdapter<LockItemBinding, LockDto> {
    private Integer deviceOnlineTotal;
    private Integer deviceTotal;
    private boolean isShowInfo = true;

    public void setDeviceOnlineTotal(Integer deviceOnlineTotal) {
        this.deviceOnlineTotal = deviceOnlineTotal;
    }

    public void setDeviceTotal(Integer deviceTotal) {
        this.deviceTotal = deviceTotal;
    }

    public LockAdapter(Context context) {
        super(context);
    }

    @Override
    protected void setBindingVariable(LockItemBinding binding, int position) {
    }

    @Override
    protected void onBindItem(LockItemBinding binding, LockDto item, int position) {
        if (position == 0) {
            binding.topLayout.setVisibility(View.VISIBLE);
            binding.tvAllDevice.setText(String.format(mContext.getString(R.string.all_device), deviceOnlineTotal, deviceTotal));
        }
        int lockImageSrc = R.drawable.lock_charging;
        if (item.getBattery() > 100) { // 充电中
            binding.llLockBattery.setVisibility(View.GONE);
        } else {
            binding.llLockBattery.setVisibility(View.VISIBLE);
            binding.tvLockNum.setText(String.valueOf(item.getBattery()));
            if (!TextUtils.isEmpty(item.getIcon())) {
                switch (item.getIcon()) {
                    case "lock_offline_close":
                        lockImageSrc = R.drawable.lock_offline_close_big;
                        break;
                    case "lock_offline_open":
                        lockImageSrc = R.drawable.lock_offline_open_big;
                        break;
                    case "lock_online_open":
                        lockImageSrc = R.drawable.lock_online_open_big;
                        break;
                    case "lock_online_close":
                        lockImageSrc = R.drawable.lock_online_close_big;
                        break;
                }
            }
        }
        binding.ivLock.setImageResource(lockImageSrc);
        binding.tvLockSn.setText(item.getSn());
        binding.tvLockName.setText(item.getName() != null ? item.getName() : "");
        binding.tvLockTime.setText(item.getLastAt() != null ? item.getLastAt() : "");
        binding.tvLockStatus.setText(item.getOnlineState());
        Drawable mDrawable = ContextCompat.getDrawable(mContext, R.drawable.oval_yellow_bg);
        if (mDrawable != null) {
            mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
        }
        binding.tvLockStatus.setCompoundDrawablesRelative(mDrawable, null, null, null);
        binding.tvLockAlertStatus.setText(item.getAlarmTypeName());
        binding.tvLockAlertTime.setText(String.format(mContext.getString(R.string.latest_alert_time), item.getAlarmTime() != null ? item.getAlarmTime() : ""));
        binding.tvLockAddress.setText(String.format(mContext.getString(R.string.address), item.getAddress() != null ? item.getAddress() : ""));

        Drawable expandDrawable = null;
        if (isShowInfo) {
            expandDrawable = ContextCompat.getDrawable(mContext, R.drawable.ic_expand);
        } else {
            expandDrawable = ContextCompat.getDrawable(mContext, R.drawable.ic_expand_close);
        }
        if (expandDrawable != null) {
            expandDrawable.setBounds(0, 0, expandDrawable.getIntrinsicWidth(), expandDrawable.getIntrinsicHeight());
        }
        binding.tvAllExpand.setCompoundDrawablesRelative(null, null, expandDrawable, null);

        binding.infoLayout.setVisibility(isShowInfo ? View.VISIBLE : View.GONE);
        binding.tvAllExpand.setTextColor(isShowInfo ? mContext.getColor(R.color.color_main) : mContext.getColor(R.color.color_text));
    }

    @Override
    protected void onCreateView(LockItemBinding binding, BaseViewHolder<LockItemBinding> holder) {
        binding.tvAllExpand.setOnClickListener(view -> {
            isShowInfo = !isShowInfo;
            notifyDataSetChanged();
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.lock_item;
    }
}
